//
//  ShopSettingViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 27..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ShopSettingViewController.h"


@implementation ShopSettingViewController
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }   
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    self.title = @"거래처 관리";    
    
    [self.tableView setAllowsSelectionDuringEditing:YES];
    
    [super viewDidLoad];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    [super viewDidUnload];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.dataSourceArray = [[SQLHandler sharedInstance] findAllShop];    
    [self.tableView reloadData];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    return YES;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showEditMode{
    _isEditMode = YES;
    [self.tableView setEditing:YES animated:YES];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                              target:self
                                                                              action:@selector(showDoneMode)];
    [self.navigationItem setRightBarButtonItem:rightBtn];        
    [rightBtn release];
}

#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addAction{
    ShopFormViewController* viewController = [[[ShopFormViewController alloc] initWithNibName:@"ShopFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController createMode];
}
#pragma mark -
#pragma mark UITableViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopFormViewController* viewController = [[[ShopFormViewController alloc] initWithNibName:@"ShopFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController loadShop:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"shop_id"]];        
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    SQLHandler *sql = [SQLHandler sharedInstance];
    [sql deleteShopById:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"shop_id"]];
    self.dataSourceArray = [sql findAllShop];
    [self.tableView reloadData];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
    [self moveRowAtIndexPath:fromIndexPath
                 toIndexPath:toIndexPath 
                   tableName:@"shop"];
}
@end
